package cz.farin.mandelinka.sample;

import java.util.Random;

import cz.farin.mandelinka.Bug;
import cz.farin.mandelinka.Movement;
import cz.farin.mandelinka.Surroundings;

public class RandomBug implements Bug {
	
	private Random random = new Random();

	@Override
	public String getName() {		
		return "Random Bug";
	}

	@Override
	public Movement move(Surroundings surroundings) {
		int r = random.nextInt(10);
		if (r < 5) {		
			if (surroundings.getForward().canWalk()) {
				return Movement.MOVE_FORWARD;
			} else {
				r += 5;
			}
		}
		//r is [5,9]		
		if (r == 8 || r == 9) {
			if (surroundings.getLeft().canWalk()) {
				return Movement.TURN_LEFT;
			}
		}
		if (r == 6 || r == 7) { 
			if (surroundings.getRight().canWalk()) {
				return Movement.TURN_RIGHT;
			}
		}		
		if (surroundings.getBackward().canWalk()) {
			return Movement.MOVE_BACKWARD;
		} else {
			return Movement.NO_MOVE;
		}		
	}

}
